import { getCampusSelector, getCampusTrain } from '@/api/campus/campus'
import request from '@/utils/request'

// import { getGoodsSelector } from '@/api/services/goods'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        campusId: undefined,
        orderTitle: undefined,
        description: undefined,
        memo: undefined,
        teachers: [],
        payAmount: 0,
        refund: 0,
        type: 0,
        method: 0
      },
      rules: {
        type: [
          {
            required: true,
            message: '请选择退款类型',
            trigger: 'change'
          }
        ],
        refund: [
          {
            required: true,
            message: '请输入价格',
            trigger: 'blur'
          },
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: '请输入正确的金额',
            trigger: 'blur'
          }
        ],
        method: [
          {
            required: true,
            message: '请选择退款方式',
            trigger: 'change'
          }
        ]
      },
      campusOptions: [],
      sourceOptions: [{ fullName: '订单', id: '1' }, { fullName: '赠送', id: '2' }],
      goodsOptions: [],
      matheVersionOptions: [{ fullName: '人教', id: '1' }, { fullName: '冀教', id: '2' }, { fullName: '北师大', id: '3' }, { fullName: '青教', id: '4' }, { fullName: '苏教', id: '5' }],
      listQuery: {
        currentPage: 1,
        pageSize: 10000,
        sort: 'desc',
        sidx: ''
      },
      items: []
    }
  },
  computed: {},
  watch: {},
  created() {
    getCampusSelector().then(res => {
      this.campusOptions = this.isDetail ? res.data.filter(item => item.stateMark == 1) : res.data
    })
    getCampusTrain(this.listQuery).then(res => {
      this.goodsOptions = res.data.list
    })
  },
  mounted() {},
  methods: {
    handleSelectionChange(val) {
      this.items = val
      /* this.dataForm.refundAmount = 0
      val.map(item => {
        this.dataForm.refundAmount += item.amount
      })*/
    },
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/train/order/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            res.data.teachers = res.data.teachers.filter(i => i.status == 'Audit' || i.status == 'Signed')
            this.dataForm = Object.assign(this.dataForm, res.data)
          })
        }
      })
    },
    dataFormSubmit() {
      let that = this
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          this.loading = true
          let memo = this.dataForm.memo
          const dropoutNames = this.items.map(item => item.teacherName)
          if (dropoutNames.length > 0) {
            memo =
              that.dataForm.memo ||
              that.dataForm.memo
                .split(',')
                .map(name => {
                  if (dropoutNames.includes(name)) {
                    return `${name}（退训）`
                  }
                  return name
                })
                .join(',')
          }
          let data = {
            type: this.dataForm.type,
            items: this.items.map(item => item.id).join(','),
            memo: memo,
            teacherNames: this.items.map(item => item.teacherName).join(','),
            reason: this.dataForm.reason,
            method: this.dataForm.method,
            refund: this.dataForm.refund
          }
          request({
            url: '/api/train/order/' + this.dataForm.id + '/refund',
            method: 'POST',
            data: data
          })
            .then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.loading = false
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
